package com.ld.security.common.core.util;

import cn.hutool.core.util.HashUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.ld.security.common.core.anno.IdKey;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

public class UuidUtil {


    public static Object initIdKey(Object obj){


        ReflectionUtil.applyAllDeclaredFields(obj,IdKey.class,(anno,name,val) -> {
            //字符特殊处理
            if(val instanceof String){
             if(StringUtil.isNotNull(val.toString())){
                 return;
             }
            }else if(val != null){
                return;
            }
            StringBuffer sb =   new StringBuffer();
            for (String s : anno.value()) {
                Object v = ReflectionUtil.invokeGetter(obj,s);

                sb.append(v);
            }
            String id = getId(sb.toString());
            ReflectionUtil.invokeSetter(obj,name,id);
        });

        return obj;
    }




    public static String getId(String... ids){
        StringBuffer sb =  new StringBuffer();
        for (String id : ids) {
            sb.append(id);
        }
        return HashUtil.murmur64(sb.toString().getBytes())+"";
    }

    public static String getId(){
        return IdUtil.objectId();
    }

    public static String md5(String key){
        return SecureUtil.md5(key);
    }


}
